package com.java.mybatis.controller;

import com.java.mybatis.entity.OutboundDetails;
import com.java.mybatis.service.OutboundDetailsServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * (OutboundDetails)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/OutboundDetails/")
public class OutboundDetailsController {
    @Autowired
    private OutboundDetailsServices outboundDetailsServices;

    @RequestMapping("queryOutboundDetails")
    public String queryOutboundDetails(HttpServletRequest request, Model model, Integer page){
        //移除模糊查询session
        request.getSession().removeAttribute("likeName");
        //调用方法  查询数据
        List<OutboundDetails> outboundList = outboundDetailsServices.queryOutboundDetailsLimit( (page - 1) * 7, 7);
        //存入数据
        model.addAttribute("outboundDetails",outboundList);
        //存入当前页
        request.getSession().setAttribute("thisPage",page);
        //总数据条数
        int count = outboundDetailsServices.queryOutboundDetailsCount();
        //存入总数据
        request.getSession().setAttribute("count",count);
        //总共有多少页
        int goodPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
        model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
        //存入总共有多少页
        model.addAttribute("goodPageMax",goodPageMax);
        //存入总共多少条数据
        model.addAttribute("count",count);
        return "System/sys-log";
    }

    /**
     * 模糊查询
     * @param request
     * @param model
     * @param state
     * @return
     */
    @RequestMapping("queryOutboundDetailsLike")
    public String queryOutboundDetailsLike(HttpServletRequest request, Model model,Integer state,Integer page, String str){
        if (str!=null&&str!="") {
            request.getSession().setAttribute("likeName",str);
            request.getSession().setAttribute("state",state);
            //调用方法  查询数据
            List<OutboundDetails> outboundDetails=null;
            HashMap<String, Object> map = new HashMap<>();
            map.put("str","%"+str+"%");
            map.put("page",(page-1)*7);
            map.put("pageSize",7);
            int count =0;
            if (state>0) {
                outboundDetails=outboundDetailsServices.queryOutboundDetailsLikeOutboundNumber(map);
                count=outboundDetailsServices.queryOutboundDetailsLikeCount1("%" + str + "%");
            }else{
                outboundDetails=outboundDetailsServices.queryOutboundDetailsLikeGoodName(map);
                count=outboundDetailsServices.queryOutboundDetailsLikeCount("%" + str + "%");
            }
            //存入数据
            model.addAttribute("outboundDetails",outboundDetails);
            //存入当前页
            request.getSession().setAttribute("thisPage",page);
            //存入总数据
            int goodPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
            System.out.println("---------------------------------"+count);
            System.out.println("---------------------------------"+goodPageMax);
            request.getSession().setAttribute("count",count);
            model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
            //存入总共有多少页
            model.addAttribute("goodPageMax",goodPageMax);
            //存入总共多少条数据
            model.addAttribute("count",count);
            return "System/sys-log";
        }else {
            return "redirect:/OutboundDetails/queryOutboundDetails?page=1";
        }
    }

}
